package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcSanitationService;
import com.csun.cmny.provider.model.dto.sanitation.*;
import com.csun.cmny.provider.model.vo.sanitation.SanitationRecordVo;
import com.csun.cmny.provider.model.vo.sanitation.SanitationServiceVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcOldmanSanitationRecordService;
import com.csun.cmny.provider.service.OmcSanitationServiceBindService;
import com.csun.cmny.provider.service.OmcSanitationServiceService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/sanitation", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSanitationController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcSanitationServiceService serviceService;

    @Resource
    private OmcOldmanSanitationRecordService recordService;

    /*新增卫生服务*/
    @ValidateAnnotation
    @RequestMapping(value = "/addSanitationService", method = RequestMethod.POST)
    public Wrapper addSanitationService (@RequestBody @Valid SanitationServiceSaveDto sanitationServiceSaveDto, BindingResult br){

        OmcSanitationService omcSanitationService = new OmcSanitationService();

        omcSanitationService.setServiceName(sanitationServiceSaveDto.getServiceName());

        List<OmcSanitationService> select = serviceService.select(omcSanitationService);
        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("sanitation.serviceName.error", null));
        }

        BeanUtils.copyProperties(sanitationServiceSaveDto,omcSanitationService);

        omcSanitationService.setGroupId(getLoginAuthDto().getGroupId());
        omcSanitationService.setCreator(getLoginAuthDto().getUserName());
        omcSanitationService.setCreatorId(getLoginAuthDto().getUserId());

        int result = serviceService.save(omcSanitationService);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*查看本机构所有卫生服务*/
    @ValidateAnnotation
    @RequestMapping(value = "/selectSanitationService", method = RequestMethod.POST)
    public Wrapper selectSanitationService (@RequestBody SanitationServiceSelectDto sanitationServiceSelectDto){


        List<SanitationServiceVo>list = serviceService.selectSanitationService(sanitationServiceSelectDto);

        PageInfo<SanitationServiceVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /*修改卫生服务*/
    @ValidateAnnotation
    @RequestMapping(value = "/updateSanitationService", method = RequestMethod.POST)
    public Wrapper updateSanitationService (@RequestBody @Valid SanitationServiceUpdateDto sanitationServiceUpdateDto, BindingResult br){

        OmcSanitationService omcSanitationService = new OmcSanitationService();

        omcSanitationService.setServiceName(sanitationServiceUpdateDto.getServiceName());

        List<OmcSanitationService> select = serviceService.select(omcSanitationService);
        if (PublicUtil.isNotEmpty(select)){
            for (OmcSanitationService sanitationService : select) {
                if (!sanitationService.getId().equals(sanitationServiceUpdateDto.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("sanitation.serviceName.error", null));
                }
            }
        }
        BeanUtils.copyProperties(sanitationServiceUpdateDto,omcSanitationService);

        int result = serviceService.update(omcSanitationService);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*删除卫生服务*/
    @ValidateAnnotation
    @RequestMapping(value = "/deleteSanitationService", method = RequestMethod.POST)
    public Wrapper deleteSanitationService (Long id){

        int result = serviceService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*新增卫生信息*/
    @ValidateAnnotation
    @RequestMapping(value = "/addSanitationRecord", method = RequestMethod.POST)
    public Wrapper addSanitationRecord (@RequestBody @Valid SanitationRecordSaveDto sanitationRecordSaveDto, BindingResult br){

        int result=recordService.addSanitationRecord(sanitationRecordSaveDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*修改卫生信息*/
    @ValidateAnnotation
    @RequestMapping(value = "/updateSanitationRecord", method = RequestMethod.POST)
    public Wrapper updateSanitationRecord (@RequestBody @Valid SanitationRecordUpdateDto sanitationRecordUpdateDto, BindingResult br){

        int result=recordService.updateSanitationRecord(sanitationRecordUpdateDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /*查看卫生信息*/
    @ValidateAnnotation
    @RequestMapping(value = "/selectSanitationRecord", method = RequestMethod.POST)
    public Wrapper selectSanitationRecord (@RequestBody @Valid SanitationRecordSelectDto sanitationRecordSaveDto, BindingResult br){

        List<SanitationRecordVo>list=recordService.selectSanitationRecord(sanitationRecordSaveDto);

        PageInfo<SanitationRecordVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


}
